<template>
  <transition name='dialog_fade'>
    <!-- 遮罩 .self事件，点击自己触发，点击子元素不触发-->
    <div class="q-dialog__wrapper"
         v-show="visible"
         @click.self="_handleClose">
      <!-- dialog组件 -->
      <div class="q-dialog"
           :style="{width:width,marginTop:top}">
        <!-- 标题头 -->
        <div class="q-dialog__header">
          <slot name="title">
            <span class="q-dialog__title">
              {{title}}
            </span>
          </slot>
          <span class="q-dialog__headerbtn"
                @click.self="_handleClose">X</span>
        </div>
        <!-- 默认插槽body -->
        <div class="q-dialog__body">
          <slot></slot>
        </div>
        <!-- footer -->
        <div class="q-dialog__footer"
             v-if="$slots.footer">
          <slot name="footer">
          </slot>
        </div>
      </div>
    </div>
  </transition>

</template>

<script>
/**
 * 模型简化 弹窗
 * @description
 * @property {Boolean}        visible         显示弹窗
 * @property {String}         title           标题
 * @property {String|Number}  width           弹窗宽度(默认'363px')
 * @property {String|Number}  height          弹窗高度(默认auto)
 * @event    {Function}       cancel          关闭弹窗触发，当前组件为右上角x号
 */
export default {
  name: 'QDialog',
  props: {
    title: {
      type: String,
      default: '提示'
    },
    width: {
      type: String,
      default: '50%'
    },
    top: {
      type: String,
      default: '15vh'
    },
    footer: {
      type: Object
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    _handleClose () {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang='scss' scoped>
/*
    scoped会给 当前组件模板中所有元素添加一个随机属性，比如data-v-xxx，并将此属性添加到样式中
    scoped会给 当前组件模板中所有元素添加一个对应的属性选择器，
*/
.q-dialog__wrapper {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  z-index: 2001;
  background-color: rgba(0, 0, 0, 0.5);
  .q-dialog {
    position: relative;
    margin: 15vh auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    box-sizing: border-box;
    width: 30%;
    &__header {
      padding: 20px 20px 10px;
      .q-dialog__title {
        line-height: 24px;
        font-size: 18px;
        color: #303133;
      }
      .q-dialog__headerbtn {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
      }
    }
    &__body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    &__footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
      /* see the vue-loader doc  */
      ::v-deep .q-button:first-child {
        margin-right: 20px;
      }
    }
  }
}
// dalog动画
.dialog_fade-enter-active {
  animation: fade 0.5s;
}
.dialog_fade-leave-active {
  animation: fade 0.2s reverse;
}
@keyframes fade {
  0% {
    opacity: 0;
    transform: translateY(-10px);
  }
  100% {
    opacity: 1;
    transform: translateY(10px);
  }
}
</style>
